using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEngine.UI;
using DG.Tweening;

public class TwoOnePanel : MonoBehaviour
{
    List<TPathData> pathDatas;
    public Transform Group, UpGroup, DownGeoup;
    Image Temp;
    public float skewing;
    public int my_Index = 0, Move_Index;
    List<Image> images = new List<Image>();
    public Image playerImage;
    // Start is called before the first frame update
    void Start()
    {
        pathDatas = JsonConvert.DeserializeObject<List<TPathData>>(Resources.Load<TextAsset>("TPathData").text);

        for (int i = 0; i < pathDatas.Count; i++)
        {
            for (int j = i + 1; j < pathDatas.Count; j++)
            {
                if (pathDatas[i].x == pathDatas[j].x && pathDatas[i].y == pathDatas[j].y)
                {
                    Debug.Log("有重复数据: " + pathDatas[i].id + "  " + pathDatas[j].id);
                }
            }
        }

        for (int i = 0; i < pathDatas.Count; i++)
        {
            CreateImage(pathDatas[i], i);
        }
        playerImage.transform.position = GetPos(pathDatas[0].x, pathDatas[0].y);
        my_Index = 0;
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void CreateImage(TPathData pathData, int _i)
    {
        if (Temp == null)
        {
            Temp = Instantiate(Resources.Load<Image>("Image"), UpGroup);
            Temp.GetComponentInChildren<Text>().text = "起点";
            Temp.transform.position = GetPos(pathData.x, pathData.y);
            images.Add(Temp);
            if (pathData.next != 0)
            {

                Temp.transform.GetChild(0).gameObject.SetActive(false);
                Temp.transform.GetChild(1).gameObject.SetActive(true);
            }
        }
        else
        {
            var image = Instantiate(Resources.Load<Image>("Image"), UpGroup);
            image.GetComponentInChildren<Text>().text = $"{my_Index++}";
            if (_i == pathDatas.Count - 1)
            {
                image.GetComponentInChildren<Text>().text = "终点";

            }
            image.transform.position = GetPos(pathData.x, pathData.y);
            var min = Instantiate(Resources.Load<Image>("Min"), DownGeoup);
            min.transform.position = Vector3.Lerp(Temp.transform.position, image.transform.position, 0.5f);

            min.GetComponent<RectTransform>().sizeDelta = new Vector2(10, Vector3.Distance(Temp.transform.position, image.transform.position));

            var minpos = new Vector3(0, 0, Vector3.Angle(image.transform.up, image.transform.position - Temp.transform.position));

            if (image.transform.position.x > Temp.transform.position.x)
            {
                minpos = -minpos;
            }
            min.transform.eulerAngles = minpos;

            Temp = image;
            images.Add(Temp);
            if (pathData.next != 0)
            {

                Temp.transform.GetChild(0).gameObject.SetActive(false);
                Temp.transform.GetChild(1).gameObject.SetActive(true);
            }

        }
    }
    public Vector3 GetPos(float x, float y)
    {
        return new Vector3(x * 130 + 40, y * 70 + 40, 0) + Group.transform.position;
    }
    public void MoveBut()
    {
        if (my_Index >= images.Count) return;
        if (!TwoPanel.Ins.TipePanel.gameObject.activeSelf && Move_Index == 0)
        {
            Move_Index = Random.Range(1, 7);

            TwoPanel.Ins.TipePaneltext.text = $"获取移动值: {Move_Index}步";
            TwoPanel.Ins.TipePanel.gameObject.SetActive(true);
            StartCoroutine("StartMove");
            MessMgr.GetSinleton().BroadCast("TwoTwoPanel");
        }
    }
    IEnumerator StartMove()
    {
        bool IsEnd = false;
        while (true)
        {
            my_Index++;
            if (my_Index >= images.Count)
            {
                IsEnd = true;
                break;
            }
            playerImage.transform.DOMove(images[my_Index].transform.position, 0.5f);
            // = images[Move_Index].transform.position;
            Move_Index--;
            if (Move_Index <= 0)
            {
                break;
            }
            yield return new WaitForSeconds(0.5f);
        }

        if (IsEnd)
        {
            TwoPanel.Ins.TipePaneltext.text = $"到达终点，恭喜你！获得1000金币\n并且获得旅行券×10";
            TwoPanel.Ins.MoneyNum += 1000;
            TwoPanel.Ins.MoveNum += 10;
            TwoPanel.Ins.TipePanel.gameObject.SetActive(true);
            my_Index = 0; // Reset index after reaching the end
            Move_Index = 0; // Reset move index
            playerImage.transform.DOMove(images[my_Index].transform.position, 0.5f);
            refreshData();
        }
        else
        {
            if (pathDatas[my_Index].next != 0)
            {

                TwoPanel.Ins.TipePaneltext.text = $"获取奖励: {pathDatas[my_Index].next}";
                TwoPanel.Ins.MoneyNum += pathDatas[my_Index].next;
                TwoPanel.Ins.TipePanel.gameObject.SetActive(true);
                images[my_Index].transform.GetChild(1).gameObject.SetActive(false);
                images[my_Index].transform.GetChild(0).gameObject.SetActive(true);
            }
            else
            {
                TwoPanel.Ins.TipePaneltext.text = $"什么奖励也没有，继续努力吧！";
                TwoPanel.Ins.TipePanel.gameObject.SetActive(true);
            }
        }
    }

    private void refreshData()
    {
        for (int i = 1; i < pathDatas.Count - 1; i++)
        {
            int index = Random.Range(0, 5);
            if (index == 0)
            {

                pathDatas[i].next = 100;
                images[i].transform.GetChild(0).gameObject.SetActive(false);
                images[i].transform.GetChild(1).gameObject.SetActive(true);
            }
            else
            {
                pathDatas[i].next = 0;
            }
        }
    }
}
public class TPathData
{
    public int id;
    public float x;
    public float y;
    public int next;
}
